//----------------------------------
//--------Alyxgun SWEP--------------
//-----------Made by Brian Nevec---- -- Finished by Luafox -- Firemode indicator by Foss
//----------------------------------
//---Part of the SMOD Weapons Pack--
//----------------------------------

//if on server
if SERVER then

	//add the file
	AddCSLuaFile("shared.lua");

	//set the hold type
	SWEP.HoldType			= "pistol";

end

//if on client
if CLIENT then

	//set some stuff
	SWEP.PrintName			= "Alyx Gun";
	SWEP.Author				= "Brian Nevec";
	SWEP.Slot				= 1;
	SWEP.SlotPos			= 3;
	SWEP.Purpose			= "Alyx got killed..";
	SWEP.Instructions		= "Left Click: Fire, Right Click: Change fire mode";

	//set the category
	SWEP.Category			= "SMOD";
	
	//selection icon
	SWEP.WepSelectIcon = surface.GetTextureID("weapons/alyxgunsmodicon");

	-- Kill Icon
	if(file.Exists("../materials/weapons/alyxgunsmod_killicon.vmt")) then

		killicon.Add("weapon_alyxgun_smod","weapons/alyxgunsmod_killicon",Color(255,255,255));

	end 

end

//is it spawnable?
SWEP.Spawnable				= true;
SWEP.AdminSpawnable			= false;

//models
SWEP.ViewModel				= "models/weapons/v_alyxgun.mdl";
SWEP.WorldModel				= "models/weapons/W_Alyx_Gun.mdl";

//more stuff
SWEP.Weight					= 25;
SWEP.AutoSwitchTo			= false;
SWEP.AutoSwitchFrom			= false;

//primary settings
SWEP.Primary =
{
	ClipSize				= 25,
	DefaultClip				= 25,
	Automatic				= false,
	Ammo					= "pistol"
}

//secondary settings
SWEP.Secondary =
{
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= false,
	Ammo					= "none"
}

//fire mode
SWEP.FireMode = 1;

//on initialize
function SWEP:Initialize()

	//if on server
	if SERVER then

		//set the hold type
		self:SetWeaponHoldType(self.HoldType);
		
		self.Weapon:SetNWInt( "smod_FireMode", self.Weapon:GetNWInt( "smod_FireMode", 1 ) );
		
	end

end

function SWEP:Precache()
util.PrecacheSound("weapons/alyxgun/fire01.wav")
util.PrecacheSound("weapons/alyxgun/fire02.wav")
util.PrecacheSound("weapons/smg1/smg1_reload.wav")
util.PrecacheSound("weapons/smg1/switch_single.wav")
end

//when we want to attack with the primary
function SWEP:PrimaryAttack()

	//if we can
	if self.Weapon:CanPrimaryAttack() then

		if self.FireMode == 1 then

			self:FireBullet(Vector(0.02,0.02,0));
					local Sound =
				{
					"weapons/alyxgun/fire01.wav",
					"weapons/alyxgun/fire02.wav"
				} 

		self.Weapon:EmitSound(Sound[math.random(1,2)])

			//delay the fire
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.14);

		elseif self.FireMode == 2 then

			for I = 1,3 do

				timer.Simple(I * 0.125,
				function()

					if ValidEntity(self.Weapon) && ValidEntity(self.Owner) then

						self:FireBullet(Vector(0.045,0.045,0));
						
						if self:CanPrimaryAttack() then
								local Sound =
				{
					"weapons/alyxgun/fire01.wav",
					"weapons/alyxgun/fire02.wav"
				} 

						self.Weapon:EmitSound(Sound[math.random(1,2)])
						end
					end

				end)

			end

			//delay the fire
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.8);

		elseif self.FireMode == 3 then

			self:FireBullet(Vector(0.04,0.04,0));
					local Sound =
				{
					"weapons/alyxgun/fire01.wav",
					"weapons/alyxgun/fire02.wav"
				} 

			self.Weapon:EmitSound(Sound[math.random(1,2)])

			//delay the fire
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.13);

		end

	end

	//return false
	return false;

end

function SWEP:FireBullet(Spread)
	if self:CanPrimaryAttack() then
	//create a bullet
	local Bullet = {};

	//set it up
	Bullet.Src = self.Owner:GetShootPos();
	Bullet.Attacker = self.Owner;
	Bullet.Dir = self.Owner:GetAimVector();
	Bullet.Spread = Spread;
	Bullet.Num = 1;
	Bullet.Damage = 5;
	Bullet.Force = 6;
	Bullet.Tracer = 1;
	Bullet.TracerName = "Tracer";

	//fire it
	self.Weapon:FireBullets(Bullet);
	self.Weapon:ShootEffects();

	//take off some ammo
	self.Weapon:TakePrimaryAmmo(1);
	end
end

function SWEP:SecondaryAttack()
	self.Weapon:EmitSound(Sound("weapons/smg1/switch_single.wav"))

	self.FireMode = self.FireMode + 1;

	if self.FireMode > 3 then

		self.FireMode = 1;

	end

	if self.FireMode == 3 || self.FireMode == 2 then

		self.Primary.Automatic = true;

	else

		self.Primary.Automatic = false;

	end

	self.Weapon:SetNWInt("smod_FireMode",self.FireMode);

	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK);

	self.Weapon:SetNextSecondaryFire(CurTime() + 0.5);

end

//when reloading
function SWEP:Reload()

	//reload
	self.Weapon:DefaultReload(ACT_VM_RELOAD);
	if self.Weapon:Clip1() != 25 then
	self.Weapon:EmitSound(Sound("weapons/smg1/smg1_reload.wav"))
	end
	//stop
	return false;

end

//when we deploy
function SWEP:Deploy()

	//run the deploy animation
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW);

	//and return
	return true;

end

//when we holster
function SWEP:Holster()
	//and return
	return true;

end

// don't do anyting on the server after this
if( !CLIENT ) then return; end

SWEP.CornerTexture = surface.GetTextureID( "gui/corner8" );

SWEP.Hud =
{
	
	// TODO: make this a bit more darker
	Color = Color( 255, 0, 0, 155 ),
	
	FrameInset = 2.0,
	FrameInsetIn = 2.0,
	
	BulletWide = 12.0,
	BulletTall = 8.0,
	
	FrameX = 0.97,
	
	// this one needs to be tweaked a bit
	// it works fine in 4:3, but doesn't
	// in 16:9 and i didn't test it in
	// 16:10
	// Valve's huds are scaled accordingly to 640x480
	// so this needs some more tweaking
	FrameY = 0.895
	
};

/*----------------------------------------------------
	Purpose: draw a bullet element
----------------------------------------------------*/
function SWEP:DrawBullet( x, y, width, height, color )
	
	surface.SetDrawColor( color.r, color.g, color.b, color.a );
	
	surface.SetTexture( self.CornerTexture );
	
	local corner_size = height * 0.5;
	local corner_size_h = corner_size * 0.5;
	local back_width = width - corner_size;
	
	surface.DrawRect( x + corner_size, y, back_width, height );
	
	surface.DrawTexturedRectRotated( x + corner_size_h, y + corner_size_h, corner_size, corner_size, 0 );
	surface.DrawTexturedRectRotated( x + corner_size_h, y + corner_size_h + corner_size, corner_size, corner_size, 90 );
	
end

/*----------------------------------------------------
	Purpose: draw a frame element
----------------------------------------------------*/
function SWEP:DrawFrame( x, y, width, height, inset, color )
	
	surface.SetDrawColor( color.r, color.g, color.b, color.a );
	
	surface.DrawRect( x, y, width, inset );
	surface.DrawRect( x, y + inset, inset, height - inset * 2 );
	surface.DrawRect( x + width - inset, y + inset, inset, height - inset * 2 );
	surface.DrawRect( x, y + height - inset, width, inset );
	
end

/*----------------------------------------------------
	Purpose: draw the firemode indicator
----------------------------------------------------*/
function SWEP:DrawHUD( )
	
	local firemode = self.Weapon:GetNWInt( "smod_FireMode" );
	
	local frame_wide = self.Hud.FrameInset * 2 + self.Hud.FrameInsetIn * 6 + self.Hud.BulletWide * 5;
	local frame_tall = self.Hud.FrameInset * 2 + self.Hud.FrameInsetIn * 2 + self.Hud.BulletTall;
	
	local x = ( ScrW( ) * self.Hud.FrameX ) - frame_wide;
	local y = ( ScrH( ) * self.Hud.FrameY ) - frame_tall;
	
	self:DrawFrame( x, y, frame_wide, frame_tall, self.Hud.FrameInset, self.Hud.Color );
	
	local bullet_x = x + self.Hud.FrameInset + self.Hud.FrameInsetIn;
	local bullet_y = y + self.Hud.FrameInset + self.Hud.FrameInsetIn;
	
	for i = 1, 5 do
		
		local cond_1 = ( firemode == 1 and i == 3 );
		local cond_2 = ( firemode == 2 and i >= 2 and i <= 4 );
		local cond_3 = ( firemode == 3 );
		
		if( cond_1 or cond_2 or cond_3 ) then
			
			self:DrawBullet( bullet_x, bullet_y, self.Hud.BulletWide, self.Hud.BulletTall, self.Hud.Color );
			
		end
		
		bullet_x = bullet_x + self.Hud.FrameInsetIn + self.Hud.BulletWide;
		
	end
	
end

// Foss' code - doesn't seem to work properly
/*

if CLIENT then

	local x = ScrW() * 0.90
	local y = ScrH() * 0.86
	
	local tx = ScrW() * 0.934
	
	local w1 = ScreenScale( 49 )
	local h1 = ScreenScale( 2.05 )
	
	local w2 = ScreenScale( 2.05 )
	local h2 = ScreenScale( 14 )
	
	surface.CreateFont( "HalfLife2", 16, 300, true, false, "alyx_gun_firemode" )

	function SWEP:DrawHUD()

		local Mode = self.Weapon:GetNWInt( "smod_FireMode", 1 )
		local Text = ""

		if Mode == 1 then

			Text = "p"
			tx = ScrW() * 0.934

		elseif Mode == 2 then

			Text = "ppp"
			tx = ScrW() * 0.9208

		elseif Mode == 3 then

			Text = "ppppp"
			tx = ScrW() * 0.91

		end

		surface.SetDrawColor( 255, 0, 0, 255 )
		
		surface.DrawRect( x, y, w1, h1 )
		surface.DrawRect( x, y + 20, w1, h1 )
		
		surface.DrawRect( x, y, w2, h2 )
		surface.DrawRect( x + 74, y, w2, h2 )
		
		surface.SetTextPos( tx, y )
		surface.SetTextColor( 255, 0, 0, 255 )
		surface.SetFont( "alyx_gun_firemode" )
		
		surface.DrawText( Text )

	end

end

*/